package cn.net.xyan

import org.junit.Test
import org.mozilla.javascript.Context
import org.mozilla.javascript.ScriptableObject
import org.mozilla.javascript.tools.shell.Global
import java.io.InputStreamReader

class JSTest {


    @Test
    fun test1() {
        var cx = Context.enter()
        cx.optimizationLevel = -1
        cx.languageVersion = Context.VERSION_ES6

        //val scope = cx.initSafeStandardObjects()

        var scope: ScriptableObject = Global(cx)


        ScriptableObject.defineClass(scope, ConsoleScriptableObject::class.java)
        val console = cx.newObject(scope,"Console")
        scope.put("console",scope,console)


        try {

           // cx.evaluateString(scope,"print(ArrayBuffer)","demo",0,null)

            val resourceLib = resource("lib/kotlin.js")
            val resourceACL = resource("acl-runtime.js")
            var resourceRead = InputStreamReader(resourceLib, Charsets.UTF_8)

            resourceRead.use {
                cx.evaluateReader(scope, resourceRead, "kotlin", 0, null)
            }
            resourceRead = InputStreamReader(resourceACL, Charsets.UTF_8)

            resourceRead.use {
                cx.evaluateReader(scope, resourceRead, "acl", 0, null)
            }
        } finally {
            Context.exit()
        }

    }
}